using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SportsStore.Models;

namespace SportsStore.Controllers
{
  public class OrderController : Controller
  {
    private IOrderRepository repository;
    private Cart cart;
    public OrderController(IOrderRepository repoService,Cart cartService)
    {
      repository = repoService;
      cart = cartService;
    }
    [Authorize]
    public ViewResult List() => View(repository.Orders.Where(o => !o.Shipped));
    [HttpPost]
    [Authorize]
    public IActionResult MarkShipped(int orderID)
    {
      Order order = repository.Orders.FirstOrDefault(o => o.OrderID == orderID);
      if (order != null)
      {
        order.Shipped = true;
        repository.SaveOrder(order);
      }
      return RedirectToAction(nameof(List));
    }
    public ViewResult Checkout() => View(new Order());
    [HttpPost]
    public IActionResult CheckOut(Order order)
    {
      if (cart.Lines.Count() == 0)
      {
        ModelState.AddModelError("", "Sorry,your cart is empty!");
      }
      if (ModelState.IsValid)
      {
        order.Lines = cart.Lines.ToArray();
        repository.SaveOrder(order);
        return RedirectToAction(nameof(Completed));
      }
      else
      {
        return View(order);
      }
    }
    public ViewResult Completed()
    {
      cart.Clear();
      return View();
    }
  }
}
